from Autotests.LocalMachine.Pages.AirClient import *
from Autotests.LocalMachine.Pages.AirClient.OptionsPage import *
from Autotests.LocalMachine.Pages.AirClient.OptionsPage.AdvancedTab import Language

from Autotests.LocalMachine.Pages.AirManager import *
from Autotests.LocalMachine.Pages.AirManager.UserSettingsPage import *
#from Autotests.LocalMachine.Pages.AirManager.DeviceDetailsPage import *
from Autotests.LocalMachine.Pages.AirManager.DevicePage import *

from Autotests.BaseTest import AirClientTestCase, AirManagerTestCase
from Config.RunConfig import RunConfig, Options
import os
import time
import shutil
from Autotests.WebDriver import DRIVER
from selenium import webdriver


class ClientPages(object):
    backup = WebBackupPage()
    backup.name = "backup"
    restore = WebRestorePage()
    restore.name = "restore"
    reports = WebReportsPage()
    reports.name = "reports"
    advanced = AdvancedTab()
    advanced.name = "advanced"
    bandwidth = BandwidthTab()
    bandwidth.name = "bandwidth"
    filterTab = FilterTab()
    filterTab.name = "filter"
    general = GeneralTab()
    general.name = "general"
    lsv = LocalSpeedKickTab()
    lsv.name = "lsv"
    schedule = ScheduleTab()
    schedule.name = "schedule"
    scripts = ScriptsTab()
    scripts.name = "scripts"

class ManagerPages(object):
    mainPage = WebManagerMainPage()
    user = UserTab()
    licenseTab = LicenseTab()
    support = SupportTab()
    overview = Overview
    backup = BackupInfo
    statistics = Statistics
    recErr = ErrorsInfo


class LanguageScreenShot(AirClientTestCase):
    def runTest(self):
        sh = ScreenShotHelper()
        for lang in [Language.chinese_CN, Language.chinese_TW, Language.dutch, Language.danish, Language.english,
                     Language.finnish, Language.french, Language.german, Language.hungarian,
                     Language.italian, Language.japanese, Language.korean, Language.norwegian,
                     Language.polish, Language.portuguese, Language.russian, Language.spanish, Language.swedish]:
        # for lang in [Language.english, Language.german, Language.french, Language.italian, Language.dutch,
        #              Language.polish, Language.portuguese]:
            #lang = Language.italian
            ClientPages.advanced.setAdvancedSettings(lang)
            screenDir = sh.createScreenshotDir(lang, "Client")
            i = 0
            for page in [ClientPages.advanced, ClientPages.backup, ClientPages.bandwidth, ClientPages.filterTab,
                         ClientPages.general, ClientPages.lsv, ClientPages.restore,
                         ClientPages.schedule, ClientPages.scripts]:
                page.goToHomeLocation()
                i += 1
                sh.takeScreen(screenDir, "%s.png" % page.name)
            DRIVER().refresh()
            ClientPages.restore.performSearch("T")
            sh.takeScreen(screenDir, "restore-search.png")
            ClientPages.reports.viewReport()
            sh.takeScreen(screenDir, "reports.png")
            ClientPages.reports.viewReportInfo()
            sh.takeScreen(screenDir, "reports-info.png")
            ClientPages.schedule.viewEditSchedule()
            sh.takeScreen(screenDir, "schedule-add.png")
            ClientPages.scripts.viewEditScript()
            sh.takeScreen(screenDir, "script-add.png")


class LanguageScreenShot_Manager(AirManagerTestCase):
    def runTest(self):
        for lang in [Language.chinese_CN, Language.chinese_TW, Language.dutch, Language.danish, Language.english,
                     Language.finnish, Language.french, Language.german, Language.hungarian,
                     Language.italian, Language.japanese, Language.korean, Language.norwegian,
                     Language.polish, Language.portuguese, Language.russian, Language.spanish, Language.swedish]:
        #for lang in [Language.english, Language.finnish, Language.french, Language.german, Language.hungarian]:
        #     lang = Language.italian
            self.setCookie(lang)
            sh = ScreenShotHelper()
            screenDir = sh.createScreenshotDir(lang, "Manager")
            ManagerPages.user.viewAddUserDialog()
            sh.takeScreen(screenDir)
            for page in [ManagerPages.user, ManagerPages.licenseTab, ManagerPages.support]:
                page.goToHomeLocation()
                sh.takeScreen(screenDir)
            ManagerPages.mainPage.viewAddGroupDialog()
            sh.takeScreen(screenDir)
            ManagerPages.mainPage.viewDeleteGroupDialog()
            sh.takeScreen(screenDir)
            ManagerPages.mainPage.viewCharts()
            sh.takeScreen(screenDir, timeoutSec=3)
            # ManagerPages.mainPage.viewFilter()
            # DRIVER().save_screenshot(os.path.join(screenDir, "0.png"))
            ManagerPages.mainPage.openDevice()
            for tab in [ManagerPages.overview, ManagerPages.backup,
                        ManagerPages.statistics, ManagerPages.recErr]:
                tab()
                sh.takeScreen(screenDir)

    def setCookie(self, cookie):
        DRIVER().delete_cookie('airlang')
        DRIVER().add_cookie({'name' : 'airlang', 'value' : cookie})
        DRIVER().refresh()
        DRIVER().get(RunConfig().get(Options.AirBackup.managerAddress))


class ScreenShotHelper(object):
    def __init__(self):
        self.screenNumber = 0

    def takeScreen(self, path, filename='', timeoutSec=1):
        if filename == '':
            self.screenNumber += 1
            filename = "%s.png" % str(self.screenNumber)
        time.sleep(timeoutSec)
        DRIVER().save_screenshot(os.path.join(path, filename))

    def createScreenshotDir(self, language, pageType):
        screenDir = os.path.join(RunConfig().get(Options.AirBackup.workingDir), "Language_Screenshots",
                                 DRIVER().browser(), language, pageType)
        if os.path.exists(screenDir):
            shutil.rmtree(screenDir)
        os.makedirs(screenDir)
        return screenDir

import unittest
if __name__ == "__main__":
    unittest.main()